import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/brand.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class BrandClassifyController extends GetxController {
  RxString name = '电脑办公'.obs;

  RxList<BrandModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    if (Get.arguments != null) {
      name.value = Get.arguments['name'];
      pageList.addAll(Get.arguments['children']);
    }
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 35) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    pageList.add(BrandModel(
        name: '华为',
        imgUrl:
            'https://img.alicdn.com/img/i3/113221094/O1CN01FrNB2V1Jx6EUWND6k_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '荣耀',
        imgUrl:
            'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '小米',
        imgUrl:
            'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '华硕',
        imgUrl:
            'https://img.alicdn.com/img/i3/111683677/O1CN01XTLeBT1d27Gq5lkwu_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '宏基',
        imgUrl:
            'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '联想',
        imgUrl:
            'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '惠普',
        imgUrl:
            'https://img.alicdn.com/img/i4/13187539/O1CN01AhM7Sg25YusVFkreQ_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '戴尔',
        imgUrl:
            'https://img.alicdn.com/img/i1/4252860039/O1CN01uOuM4a1C9uMQ0k2yF_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '三星',
        imgUrl:
            'https://img.alicdn.com/imghttps://img.alicdn.com/imgextra/i4/2207907575309/O1CN01h2zklq1p5ZdQpK463_!!2207907575309-0-alimamacc.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: 'LG',
        imgUrl:
            'https://img.alicdn.com/img/i1/1487820114/O1CN01cdH39S1CiG4RhFFyV_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'));
    pageList.add(BrandModel(
        name: '苹果',
        imgUrl:
            'https://img.alicdn.com/img/i4/195765460/O1CN01KGinY81qCjPnhzuy2_!!195765460-0-alimamacc.jpg_360x360xzq75.jpg_.webp'));
  }
}

class BrandClassifyPage extends GetView<BrandClassifyController> {
  const BrandClassifyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Obx(() => Text(controller.name.value)),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return InkWell(
          onTap: () {
            Get.toNamed(RoutePath.brandManufacture, arguments: {'row': row});
          },
          child: Container(
            padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 15.h, bottom: 10.h),
            decoration: const BoxDecoration(color: Colors.white),
            child: CustomPaint(
              painter: _MyCustomPainter(),
              child: Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(5.r),
                    child: CachedNetworkImage(
                      imageUrl: row.imgUrl,
                      width: 60,
                      height: 50,
                      fit: BoxFit.fill,
                    ),
                  ),
                  Expanded(
                      child: Container(
                    padding: const EdgeInsets.only(left: 10),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      row.name,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: StylesUtils.fontSize16),
                    ),
                  ))
                ],
              ),
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}

class _MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2);
    canvas.drawLine(Offset(0, size.height + 10), Offset(size.width, size.height + 10), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
